package com.aysafety.iotdataplatformclient.acousticEmissionIntensity.services.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.AcousticEmissionIntensityModel;
import com.aysafety.iotdataplatformclient.acousticEmissionIntensity.dao.AcousticEmissionIntensityDao;
import com.aysafety.iotdataplatformclient.acousticEmissionIntensity.services.AcousticEmissionIntensityService;
import com.aysafety.iotdataplatformclient.modelConfigurer.AcousticEmissionIntensityModelConfiger;

@Service
public class AcousticEmissionIntensityServiceImpl implements AcousticEmissionIntensityService {

    @Autowired
    AcousticEmissionIntensityModelConfiger acousticEmissionIntensityModelConfiger;

    @Autowired
    AcousticEmissionIntensityDao acousticEmissionIntensityDao;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private AcousticEmissionIntensityModel transformData(Map<String, Object> temp, Map<String, String> structure) {
        AcousticEmissionIntensityModel acousticEmissionIntensityModel = new AcousticEmissionIntensityModel();
        for (String key : structure.keySet()) {
            String columnKey = structure.get(key);
            if ("EntId".equals(key)) {
                acousticEmissionIntensityModel.setEntId(acousticEmissionIntensityModelConfiger.getEntId());
            }
            if ("deviceid".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    acousticEmissionIntensityModel.setDeviceId((String) temp.get(columnKey));
                }
            }
            if ("unit".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    acousticEmissionIntensityModel.setUnit((String) temp.get(columnKey));
                }
            }
            if ("monitoringvalue".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    acousticEmissionIntensityModel.setMonitoringValue(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("poklad".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    acousticEmissionIntensityModel.setPoklad(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("datetime".equals(key)) {
                String[] columnTemp = columnKey.split("\\+");
                String dateTimeStr = "";
                for (String value : columnTemp) {
                    if (temp.containsKey(value)) {
                        dateTimeStr += temp.get(value) + " ";
                    }
                }
                dateTimeStr = dateTimeStr.trim();
                acousticEmissionIntensityModel.setDatetime(DateUtils.fomatDateTime(dateTimeStr));
            }
            if ("smallestrange".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    acousticEmissionIntensityModel.setSmallestRange(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("maximumrange".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    acousticEmissionIntensityModel.setMaximumRange(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("stationname".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    acousticEmissionIntensityModel.setStationName((String) temp.get(columnKey));
                }
            }
            if ("status".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    int status = Integer.parseInt(temp.get(columnKey).toString());
                    if (status <= 0) {
                        acousticEmissionIntensityModel.setStatus(false);
                    } else {
                        acousticEmissionIntensityModel.setStatus(true);
                    }
                }
            }
            if ("powercutarea".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    acousticEmissionIntensityModel.setPowerCutArea((String) temp.get(columnKey));
                }
            }
            if ("powerreconnectionlimit".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    acousticEmissionIntensityModel
                        .setPowerReconnectionLimit(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("powercutlimit".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    acousticEmissionIntensityModel.setPowerCutLimit(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
        }
        return acousticEmissionIntensityModel;
    }



    public int readHistroyDataCount(Map<String, Object> conditions) {
        return acousticEmissionIntensityDao.readHistroyDataCount(conditions);
    }

    public List<AcousticEmissionIntensityModel> readHistroyData(Map<String, Object> conditions, int start,
        int prePage) {
        List<AcousticEmissionIntensityModel> readHistroyDataModel = new ArrayList<AcousticEmissionIntensityModel>();
        Map<String, String> structure = acousticEmissionIntensityModelConfiger.getDatakey();
        List<Map<String, Object>> readHistroyDatas =
            acousticEmissionIntensityDao.readHistroyData(conditions, start, prePage);
        for (Map<String, Object> readHistroyData : readHistroyDatas) {
            AcousticEmissionIntensityModel acousticEmissionIntensityModel =
                this.transformData(readHistroyData, structure);
            readHistroyDataModel.add(acousticEmissionIntensityModel);
        }
        return readHistroyDataModel;
    }
}
